package com.geostar.constructionland.index.service.impl;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.entity.AddHookIndex;
import com.geostar.constructionland.index.entity.model.AddHookIndexModel;
import com.geostar.constructionland.index.entity.model.InsertAddHookIndexModel;
import com.geostar.constructionland.index.mapper.AddHookIndexMapper;
import com.geostar.constructionland.index.service.AddHookIndexService;
import com.geostar.constructionland.index.util.FormFieldsUtils;
import com.geostar.constructionland.index.util.FormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author df
 * @since 2020-2-4 10:07
 **/

@Service
public class AddHookIndexSerImpl implements AddHookIndexService {

    @Resource
    AddHookIndexMapper addHookIndexMapper;

    @Override
    @Transactional
    public IPage<Map<String,String>> queryAddHookIndexList(AddHookIndexModel addHookIndexModel) {
        QueryWrapper<AddHookIndex> queryWrapper = new QueryWrapper<>();

        System.out.println(addHookIndexModel.getSortName());
        //根据搜索框模糊查询项目名称或rkid
        queryWrapper.and(wrapper -> wrapper.like("xmmc",addHookIndexModel.getKeyWord()).or().like("rkid",addHookIndexModel.getKeyWord()));
        //查询地级市
        if(StringUtils.isNotBlank(addHookIndexModel.getDivisionCode())){
            addHookIndexModel.setDivisionCode(addHookIndexModel.getDivisionCode().replace("'",""));
            queryWrapper.in("djs", FormatUtils.getStringByList(addHookIndexModel.getDivisionCode()));
        }
        //查询入库状态
        if(StringUtils.isNotBlank(addHookIndexModel.getIndexStatus())){
            queryWrapper.in("is_rk" , FormatUtils.getStringByList(addHookIndexModel.getIndexStatus()));
        }
        if(!StringUtils.isBlank(addHookIndexModel.getSortOrder())){
            if (addHookIndexModel.getSortOrder().equals("desc") ?  true:false ){
                    queryWrapper.orderByDesc(addHookIndexModel.getSortName());
            }else{
                    queryWrapper.orderByAsc(addHookIndexModel.getSortName());
            }
            }
//        //根据djs排序
            queryWrapper.orderByAsc("djs");


        return addHookIndexMapper.queryAddHookIndexByWrapper(new Page<>(addHookIndexModel.getPagenum(),addHookIndexModel.getPagesize()),queryWrapper);
    }


    @Override
    @Transactional
    public int delAddHookIndexByIds(List<String> idList) {

        return  addHookIndexMapper.deleteBatchIds(idList);

    }

    @Override
    public AddHookIndex queryAddHookIndex(String id) {
        return addHookIndexMapper.selectById(id);
    }

    @Override
    public int addOrUpdateAddHookIndex(InsertAddHookIndexModel insertAddHookIndexModel) {
        AddHookIndex addHookIndex=new AddHookIndex();
            addHookIndex.setDivisionCode(insertAddHookIndexModel.getDivisionCode());//地级市
            addHookIndex.setDivisionName(insertAddHookIndexModel.getDivisionName());//地级市名称
            addHookIndex.setWingName(insertAddHookIndexModel.getWingName());//县市区（行政区划代码）
            addHookIndex.setProjectName(insertAddHookIndexModel.getProjectName());//项目名称
            addHookIndex.setApprovalAuthority(insertAddHookIndexModel.getApprovalAuthority());//批准机关
//            addHookIndex.setApprovalTime(insertAddHookIndexModel.getApprovalTime());//批准时间
            addHookIndex.setApprovalNum(insertAddHookIndexModel.getApprovalNum());//批复文号
            addHookIndex.setApprovalArea(insertAddHookIndexModel.getApprovalArea());//批准面积
            addHookIndex.setBuildLandAdd(insertAddHookIndexModel.getBuildLandAdd());//新增建设用地(BigDecimal)
            addHookIndex.setAgclLand(insertAddHookIndexModel.getAgclLand());//农用地(BigDecimal)
            addHookIndex.setFarmland(insertAddHookIndexModel.getFarmland());//耕地
            addHookIndex.setIsCompensationScheme(insertAddHookIndexModel.getIsCompensationScheme());//是否至今未实施征地补偿安置方案
            addHookIndex.setAreConsistent(insertAddHookIndexModel.getAreConsistent());//现场地类与批准前是否一致
            addHookIndex.setLandThatDepartment(insertAddHookIndexModel.getLandThatDepartment());//现场地类认定部门
            addHookIndex.setRemark(insertAddHookIndexModel.getRemark());//备注

        if(!StringUtils.isBlank( insertAddHookIndexModel.getId())){
            addHookIndex.setId(insertAddHookIndexModel.getId());
            addHookIndex.setCreateTime(new Date());//取当前时间
            return addHookIndexMapper.updateById(addHookIndex);
        }else{

            addHookIndex.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            addHookIndex.setUpdateTime(new Date());//修改时间
            return addHookIndexMapper.insert(addHookIndex);
        }

    }
}
